package com.zxy.health.controller;

import com.zxy.health.dao.StudentDao;
import com.zxy.health.rsp.StudentListRsp;
import com.zxy.health.service.StudentService;
import com.zxy.health.utils.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    public StudentService studentService;


    @RequestMapping("/toStudent")
    public String toTeacher(){
        return "student";
    }


    @GetMapping("/getStudentList")
    @ResponseBody
    public StudentListRsp getStudentList(){
        StudentListRsp studentListRsp = new StudentListRsp();
        studentListRsp.setList(studentService.getStudentList());
        return studentListRsp;
    }

    @RequestMapping("/getStudentByLogin")
    @ResponseBody
    public StudentDao getStudent(@RequestParam("username") String username, @RequestParam("password") String password){
        return studentService.getStudentByLogin(username,password);
    }



    @RequestMapping("/insertStudent")
    @ResponseBody
    public String insertStudent(
            @Param("name") String name,
            @Param("sex")Integer sex,
            @Param("username")String username,
            @Param("password")String password,
            @Param("schoolid")String schoolid,
            @Param("classname")String classname,
            @Param("phone")String phone
    ){
        StudentDao studentDao = new StudentDao();
        studentDao.setId(StringUtils.getSimpleUUID());
        studentDao.setName(name);
        studentDao.setSex(sex);
        studentDao.setUsername(username);
        studentDao.setPassword(password);
        studentDao.setRole(1);
        studentDao.setSchoolid(schoolid);
        studentDao.setClassname(classname);
        studentDao.setPhone(phone);
        studentDao.setHealthcode(1);
        return studentService.insertStudent(studentDao);
    }

    @RequestMapping("/getStudentBySchoolid")
    @ResponseBody
    public StudentDao getStudentBySchoolid(
            @RequestParam("schoolid") String schoolid
    ){
        return studentService.getStudentBySchoolId(schoolid);
    }

    @RequestMapping("/getStudentByClassName")
    @ResponseBody
    public StudentListRsp getStudentByClassName(
            @RequestParam("classname") String classname
    ){
        StudentListRsp studentListRsp = new StudentListRsp();
        studentListRsp.setList(studentService.getStudentByClassName(classname));
        return studentListRsp;
    }
}
